<?php
// +----------------------------------------------------------------------
// | 食得鲜 [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016 http://www.shidexian.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: 何锐 <poptser@foxmail.com>  [ ON THE WAY ]
// +----------------------------------------------------------------------
// | DateTime：2016-8-3 16:05:07
// +----------------------------------------------------------------------

namespace YMApi\Model;
use Think\Model;

class CheckregionModel extends Model
{
    /**
     * 根据ID查询详细地址
     * Author:何锐 <poptser@foxmail.com>
     * @param $address_id
     * @return bool|string
     */
    public function get_address_by_id($address_id){
        $user_address = M("user_address")->where('address_id ='.$address_id)->order('province,city,district,address')->find();
        if($user_address){
            $province = M('region')->where('region_id ='.$user_address['province'])->getField('region_name');
            $city = M('region')->where('region_id ='.$user_address['city'])->getField('region_name');
            $district = M('region')->where('region_id ='.$user_address['district'])->getField('region_name');
            return $province.$city.$district.$user_address['address'];
        }else{
            return false;
        }
    }

    /**
     * 地址转GPS坐标值
     * Author:何锐 <poptser@foxmail.com>
     * @param $address
     */
    public function address_to_gps($address){
        //http://apis.map.qq.com/ws/geocoder/v1/?address=北京市海淀区彩和坊路海淀西大街74号&key=OB4BZ-D4W3U-B7VVO-4PJWW-6TKDJ-WPB77
        $url = C('TENCENT_URL').C('TENCENT_KEY')."&address=".$address;
        $get_data = file_get_contents($url);
        $Json_Data = json_decode($get_data, true);
        $data['longitude'] = $longitude =$Json_Data['result']['location']['lng'];    //经度
        $data['latitude'] = $latitude =$Json_Data['result']['location']['lat'];     //纬度
        return $data;
    }

    /**
     * 获取用户默认地址的GPS坐标值
     * Author:何锐 <poptser@foxmail.com>
     * @param $uid
     * @return bool|void
     */
    public function get_user_default_address_gps($uid){
        $where['user_id'] = $uid;
        $where['default'] = 1;
        $user_address_id = M("user_address")->where($where)->getField('address_id');
        if ($user_address_id){
            $user_address = $this->get_address_by_id($user_address_id);
            return $this->address_to_gps($user_address);
        }else{
            return false;
        }
    }

    /**
     * 根据经纬度获取区域优惠信息
     * @param $longitude
     * @param $latitude
     */
    public function get_distribution_by_gps($longitude, $latitude){
        $polygon_free = array(
            array("lat" => 23.120083, "lng" => 113.321613),
            array("lat" => 23.140093, "lng" => 113.321889),
            array("lat" => 23.139163, "lng" => 113.340789),
            array("lat" => 23.13245, "lng" => 113.34007),
            array("lat" => 23.126002, "lng" => 113.381177),
            array("lat" => 23.116962, "lng" => 113.380746)
        );
        $polygon_99_top = array(
            array("lat" => 23.114402, "lng" => 113.321673),
            array("lat" => 23.121631, "lng" => 113.286523),
            array("lat" => 23.141539, "lng" => 113.281995),
            array("lat" => 23.149315, "lng" => 113.288858),
            array("lat" => 23.161742, "lng" => 113.314047),
            array("lat" => 23.138681, "lng" => 113.401146),
            array("lat" => 23.122928, "lng" => 113.395828),
            array("lat" => 23.13328, "lng" => 113.340618),
            array("lat" => 23.139345, "lng" => 113.341193),
            array("lat" => 23.139894, "lng" => 113.327683),
            array("lat" => 23.140591, "lng" => 113.321161)
        );
        $polygon_99_bottom = array(
            array("lat" => 23.105195, "lng" => 113.357022),
            array("lat" => 23.110447, "lng" => 113.356878),
            array("lat" => 23.113704, "lng" => 113.34423),
            array("lat" => 23.112042, "lng" => 113.323461),
            array("lat" => 23.11058, "lng" => 113.300177),
            array("lat" => 23.117294, "lng" => 113.28566),
            array("lat" => 23.107655, "lng" => 113.278977),
            array("lat" => 23.095754, "lng" => 113.284654),
            array("lat" => 23.140591, "lng" => 113.356375)
        );
        $polygon_199 = array(
            array("lat" => 23.070886, "lng" => 113.313184),
            array("lat" => 23.100076, "lng" => 113.320946),
            array("lat" => 23.09602, "lng" => 113.282857),
            array("lat" => 23.108386, "lng" => 113.277468),
            array("lat" => 23.125071, "lng" => 113.285804),
            array("lat" => 23.144679, "lng" => 113.281636),
            array("lat" => 23.153319, "lng" => 113.25943),
            array("lat" => 23.139428, "lng" => 113.237152),
            array("lat" => 23.124673, "lng" => 113.237439),
            array("lat" => 23.120551, "lng" => 113.239236),
            array("lat" => 23.114502, "lng" => 113.255908),
            array("lat" => 23.080927, "lng" => 113.279624),
            array("lat" => 23.071086, "lng" => 113.313112)
        );

        if ($this->isPointInPolygon($polygon_free,$longitude,$latitude)) {
            $str = "免费区，三小时送达";
        } else if ($this->isPointInPolygon($polygon_99_top,$longitude,$latitude)) {
            $str = "99元上半区，24小时送达";
        } else if ($this->isPointInPolygon($polygon_99_bottom,$longitude,$latitude)) {
            $str = "99元下半区，24小时送达";
        } else if ($this->isPointInPolygon($polygon_199,$longitude,$latitude)) {
            $str = "199元区，24小时送达";
        } else {
            $str = "抱歉，不在活动区内";
        }

        return $str;

    }



    public function isPointInPolygon($polygon,$longitude,$latitude){
        $count = count($polygon);
        $px = $longitude;
        $py = $latitude;
        $flag = FALSE;
        for ($i = 0, $j = $count - 1; $i < $count; $j = $i, $i++) {
            $sy = $polygon[$i]['lng'];
            $sx = $polygon[$i]['lat'];
            $ty = $polygon[$j]['lng'];
            $tx = $polygon[$j]['lat'];

            if ($px == $sx && $py == $sy || $px == $tx && $py == $ty)
                return TRUE;

            if ($sy < $py && $ty >= $py || $sy >= $py && $ty < $py) {
                $x = $sx + ($py - $sy) * ($tx - $sx) / ($ty - $sy);
                if ($x == $px)
                    return TRUE;
                if ($x > $px)
                    $flag = !$flag;
            }
        }
        return $flag;
    }












}